package com.badger.spring.boot.security.config;

import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.AnonymousConfigurer;
import org.springframework.security.config.annotation.web.configurers.FormLoginConfigurer;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("test").password(passwordEncoder().encode("123456"))
                .authorities("admin");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 1、表单操作 表单请求成功处理器、失败处理器;与loginPage冲突，配置后，loginPage不生效
        FormLoginConfigurer<HttpSecurity> formLogin = http.formLogin();
        formLogin.loginPage(null);// 自定义登录页的路由（静态页面，或者mvc视图式的页面地址）
        formLogin.loginProcessingUrl(null);// 登录接口的路由地址：必须为post请求的接口地址
        formLogin.isCustomLoginPage();// 是否为自定义登录页，true为自定义的登录页；
        formLogin.passwordParameter(null);// 登录的表单的 密码的字段名称
        formLogin.usernameParameter(null);// 登录的表单的 用户名的字段名称

        formLogin.successForwardUrl(null);// 登录成功跳转的url
        formLogin.successHandler(null);// 登录成功的拦截器
        formLogin.defaultSuccessUrl(null);// 默认的成功的url

        formLogin.failureForwardUrl(null);// 失败跳转的url
        formLogin.failureHandler(null);// 失败的拦截器
        formLogin.failureUrl(null);// 失败的url

        formLogin.permitAll();// 放开不拦截，默认为true
        formLogin.permitAll(false);

        formLogin.disable();// 关闭表单的登录(其实只是拦截器链路上的一个拦截器)

        formLogin.init(http);// 初始化方法
        formLogin.authenticationDetailsSource(null);
        formLogin.addObjectPostProcessor(null);
        formLogin.withObjectPostProcessor(null);
        HttpSecurity and = formLogin.and();
        formLogin.setBuilder(and);
        formLogin.configure(http);

        http.addFilter(null);
        http.addFilterAfter(null, null);
        http.addFilterAt(null, null);

        AnonymousConfigurer<HttpSecurity> anonymous = http.anonymous();
        http.anonymous(null);

        http.cors();
        http.cors(null);
        http.csrf();
        http.csrf(null);

        http.headers();
        http.headers(null);
        http.httpBasic();
        http.httpBasic(null);

        http.logout().addLogoutHandler(null);
        http.logout(null);

        http.oauth2Client();
        http.oauth2Client(null);
        http.oauth2Login();
        http.oauth2Login(null);
        http.oauth2ResourceServer();
        http.oauth2ResourceServer(null);

        http.rememberMe();
        http.rememberMe(null);

        http.sessionManagement();
        http.sessionManagement(null);

        http.exceptionHandling();
        http.exceptionHandling(null);
        http.userDetailsService(null);
        http.authenticationProvider(null);

        http.antMatcher(null);

        http.apply(null);

        http.authorizeRequests().antMatchers("/**/*.js", "/**/*.css").permitAll().anyRequest().hasRole("admin");
        http.authorizeRequests();
        http.authorizeRequests(null);

        http.build();
    }
}